/*
 SAMPLE FORM
*/
var qs = require('querystring');

require('./common');


exports.save = function(req, res){
    //req.params.ann
    //var tableID = req.params.tableID;
    //var requestID = req.params.requestID;
    var announcement = req.params.announcement;
    
    addAnnouncement(announcement) // call a function to Save it to the database
    
    res.send(getannouncement(announcement)) 
}

// Load announcement from the database
exports.getAnnouncement = function(req, res){
    var output="";
    var strSQL = "SELECT * FROM tbannouncement ORDER BY id DESC";
    conn.query(strSQL, function(err, results, fields){
            if(err) throw err;
            
            for (var index in results) {
                output += getannouncement(results[index].announcement);
            }
            res.send(output);
    });
}
// Announcement save to database functions
function addAnnouncement(announcement){
    var strSQL = "INSERT INTO tbannouncement (announcement) VALUES ('" + announcement + "')"
    
    if(announcement != "undefined"){
            conn.query(strSQL);
            console.log("done adding comment...")
    }
}

/* For Posting Announcement */
//announcementComment($announcementID,$userID,$userExtension,$userName,$announcement,$date)
function getannouncement(announcement){
    
    var ret = '<li class="postContainerList" id="myPost_23" style="display: list-item;">';
    ret += '    <div>';
    ret += '        <div style="float:left;">';
    ret += '            <img src="/images/avatar/avatar.png" style="float: left;height: 50px;width: 50px;margin-top: -3px;margin-right:5px; none">';
    ret += '        </div>';
    ret += '    </div>';
    ret += '    <div>';
    ret += '        <div class="requestPost">';
    ret += '            Kobe Bryant';
    ret += '        </div>';
    ret += '        <div class="requestContentPost" id="viewComment_23">';
    ret += announcement;
    ret += '        </div>';
    ret += '        <div class="requestButtonPost">';
    ret += '            <label class="hrefPostAction viewCommentBox" data-id="23">Comment</label>';
    ret += '            |';
    ret += '            <label class="hrefPostAction deletePost" data-id="23">Delete</label>';
    ret += '            <label class="fontGray timeago pull-right" style="margin-right:15px;font-size:10px;" title="2013-04-16  10:35:32">33 minutes ago</label>';
    ret += '        </div>';
    ret += '        <div class="addCommentPost" id="addComment_23_1">';
    ret += '            <div class="showMoreCommentResult_23_1"></div><div class="appendNewComment" id="appendNewComment_23_1"></div>';
    ret += '            <div class="userAvatar">';
    ret += '                <div style="float:left;">';
    ret += '                    <img src="/images/avatar/avatar.png" style="float: left;height: 30px;width: 30px;margin-top: -3px;margin-right:5px; none">';
    ret += '                </div> ';
    ret += '            </div>';
    ret += '            <div class="commentPost">';
    ret += '                <textarea class="input-comment" id="newComment_23_1" style="height:30px;width:350px;"></textarea><br>';
    ret += '                <input type="button" class="classname addButtonComment" data-comment-type="1" value="Comment" data-id="23">';
    ret += '            </div>';
    ret += '         </div>';
    ret += '    </div>';
    ret += '</li>';
    return ret;
    
}

/*
exports.save = function(req, res){

	var tblName = req.body.tblName;
	var strSQL = "SELECT COLUMN_NAME FROM information_schema.columns where table_name='" + tblName +"'";
	var fieldName = new Array();
	
	conn.query(strSQL, function(err, results, fields){
		if(err) throw err;
		
		for (var index in results) {
			fieldName[fieldName.length] = results[index].COLUMN_NAME;
		}
		console.log(fieldName.join());
		insertRecord(tblName,fieldName, req);
		res.redirect('/');
	});
}


function insertRecord(tblName, fields, req){
	var createSQL = "INSERT INTO " + tblName + " ( ";
	
	for(var ctr=1;ctr<fields.length;ctr++){
		if(ctr<fields.length-1){
		createSQL += fields[ctr] + ", ";
		}else{
		createSQL += fields[ctr] + "";
		}
	}
	
	createSQL+=") VALUES (";
	
	for(var ctr=1;ctr<fields.length;ctr++){
		if(ctr<fields.length-1){
		createSQL += "'" + req.param(fields[ctr],null) + "', ";
		}else{
		createSQL += "'" + req.param(fields[ctr],null) + "' ";
		}
	}
	
	createSQL+=")";
	
	//console.log(createSQL);
	console.log("INSERTING RECORD...." + tblName);
	conn.query(createSQL);
	console.log("Done INSERTING RECORD..." + tblName);
}*/